﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OPM.Class;
using OPM.Models;
using OPM.ServiceOfficer;

namespace OPM.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult Login()
        {
            ViewBag.userName = "Dev";
            return View();
        }

        [HttpPost]
        public ActionResult Login(string userName, string password)
        {
            return LoginPrivate(userName, password);
        }

        [HttpPost]
        public ActionResult Recovery(string citizen, string email)
        {
            var string1 = WSO.RecoveryPassword(email, citizen);
            ViewBag.message = string1;
            return View("Login");
        }
        private ActionResult LoginPrivate(string userName, string password)
        {
            User user = null;
            
            if (!string.IsNullOrWhiteSpace(userName) &&
                (user = Wb.GetUser(userName, password)) != null)
            {
                U.LoginKeepSession(user);
                UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
                string url = u.Action("Index", "Home", null);
                var profile = WSO.GetAllProfile(); 
                //string cultureChanged = (U.default_language ?? "th-TH").ToLower().StartsWith("en") ? "en-US" : "th-TH";
                string cultureChanged = (profile.default_language ?? "TH").ToUpper().StartsWith("EN") ? "EN" : "TH";
                Session["culture"] = cultureChanged;
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureChanged);

                return Content(Util.RenderTagMetaRedirect(url, "Home", profile.default_language));
            }
            ViewBag.userName = userName;
            ViewBag.message = "ข้อมูลผู้ใช้ไม่ถูกต้อง";
            return View("Login");
        }
        public ActionResult Logoff()
        {
            UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
            string url = u.Action("Login", "Account", null);
            var profile = WSO.GetAllProfile();

            return Content(Util.RenderTagMetaRedirect(url, "Login", profile.default_language));
        }

        public ActionResult ResetPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ResetPassword(ResetPassword model)
        {

            try
            {

                var reset_pass = WSO.ChangePassword(U.Token, model.old_password, model.new_password);

                if (reset_pass == "\"Err:รหัสผ่านไม่ถูกต้อง\"")
                {
                    TempData["NonSuccess"] = "รหัสผ่านไม่ถูกต้อง";
                }

                return View(model);

            }
            catch (Exception ex)
            {
                return View(model);
                TempData["NonSuccess"] = "รหัสผ่านไม่ถูกต้อง";
            }


            return RedirectToAction("Index", "PopupResult");

        }
    }
}